/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.just.dusin.ooii.core;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.just.dusin.ooii.component.Source;

/**
 *
 * @author Administrator
 */
public class Controller {

    private boolean stop = false;
    private static final Logger logger = Logger.getLogger(Controller.class.getName());
    private List<Source> sourceList;
    private List<Thread> threads = new ArrayList<Thread>();
    private boolean isAmqServerNeed;
    private MqServer mqServer = null;

    public void run() {
        try {
            if (isAmqServerNeed) {
                mqServer.start();
                logger.info("MQ 消息服务器启动");
            }
            for (Source source : sourceList) {
                if (!source.isStop()) {  //不启动停止的源组件
                    Thread t = new Thread(source);
                    t.start();
                    logger.info(source.getClass().getSimpleName() + "组件启动");
                    threads.add(t);
                }
            }
        } catch (Exception ex) {
            Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, ex);
        }
        while (!stop) {
            try {
                Thread.sleep(3000);
            } catch (InterruptedException ex) {
                logger.warning("睡眠中断");
            }
        }
    }

    public void suspend() {
    }

    public void stop() {
        logger.info("即将停止有所线程");
        for (Source t : sourceList) {
            t.stop();
            logger.info(t.getClass().getSimpleName() + "组件停止");
        }
        try {
            Thread.sleep(2000);
        } catch (InterruptedException ex) {
        }
        try {
            mqServer.stop();
        } catch (Exception ex) {
        }
        try {
            Thread.sleep(3000);
        } catch (InterruptedException ex) {
        }
        stop = true;
    }

    public boolean isIsAmqServerNeed() {
        return isAmqServerNeed;
    }

    public void setIsAmqServerNeed(boolean isAmqServerNeed) {
        this.isAmqServerNeed = isAmqServerNeed;
    }

    public List<Source> getSourceList() {
        return sourceList;
    }

    public void setSourceList(List<Source> sourceList) {
        this.sourceList = sourceList;
    }

    public MqServer getMqServer() {
        return mqServer;
    }

    public void setMqServer(MqServer mqServer) {
        this.mqServer = mqServer;
    }
}
